package com.ytclub.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.ytclub.auth.common.enums.IsDeletedFlagEnum;
import com.ytclub.auth.domain.constants.AuthConstant;
import com.ytclub.auth.domain.convent.AuthRoleBOConverter;
import com.ytclub.auth.domain.entity.AuthRoleBO;
import com.ytclub.auth.domain.redis.RedisUtil;
import com.ytclub.auth.domain.service.AuthRoleDoaminService;
import com.ytclub.auth.infra.basic.entity.AuthPermission;
import com.ytclub.auth.infra.basic.entity.AuthRole;
import com.ytclub.auth.infra.basic.service.AuthRoleService;
import com.ytclub.auth.infra.basic.service.AuthUserRoleService;
import com.ytclub.auth.infra.basic.service.AuthUserService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色领域层接口实现类
 *
 * @author: HuangQi
 * @date: Created in 21:14 2023/12/19
 * @description: 角色领域层接口实现类
 */
@Service
@Slf4j
public class AuthRoleDoaminServiceImpl implements AuthRoleDoaminService {


    @Resource
    private AuthRoleService authRoleService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private AuthUserService authUserService;

    @Resource
    private AuthUserRoleService authUserRoleService;


    /**
     * 新增角色
     *
     * @param authRoleBO 新增角色的信息
     * @return 新增结果
     */
    @Override
    public Boolean add(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleDoaminServiceImpl.add.bo:{}", JSON.toJSONString(authRoleBO));
        }

        // 参数转换
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBoToEntity(authRoleBO);
        authRole.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        // 调用service
        Integer count = authRoleService.insert(authRole);

        // 返回结果
        return count > 0;
    }

    /**
     * 更新角色
     *
     * @param authRoleBO 更新角色的信息
     * @return 更新结果
     */
    @Override
    public Boolean update(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleDoaminServiceImpl.update.bo:{}", JSON.toJSONString(authRoleBO));
        }

        // 参数转换
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBoToEntity(authRoleBO);

        // 调用service
        Integer count = authRoleService.update(authRole);

        // 返回结果
        return count > 0;
    }

    /**
     * 删除角色(逻辑删除)
     *
     * @param authRoleBO 删除角色的信息
     * @return 删除结果
     */
    @Override
    public Boolean delete(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleDoaminServiceImpl.delete.bo:{}", JSON.toJSONString(authRoleBO));
        }

        // 参数转换
        AuthRole authRole = new AuthRole();
        authRole.setId(authRoleBO.getId());
        authRole.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());

        // 调用service
        Integer count = authRoleService.update(authRole);

        // 返回结果
        return count > 0;
    }


    /**
     * 获取用户角色列表
     *
     * @param userName 用户名
     * @return 用户角色列表
     */
    @Override
    public List<String> getRole(String userName) {
        // 构建redis查询key
        String roleKey = redisUtil.buildKey(AuthConstant.AUTH_ROLE_PREFIX, userName);
        String roleValue = redisUtil.get(roleKey);
        List<String> authList = new LinkedList<>();
        if (StringUtils.isBlank(roleValue)) {
            // 如果从redis中拿到的value为空,查询数据库
            // 拿到用户id
            Long id = authUserService.queryByUserName(userName).getId();

            // 根据用户id查询角色id列表
            List<Long> roleIdList = authUserRoleService.queryByUserId(id);
            // 根据角色id列表查询角色信息
            List<AuthRole> roleList = authRoleService.queryByRoleList(roleIdList);
            redisUtil.set(roleKey, new Gson().toJson(roleList));
        }
        // 如果从redis中拿到了value,将json字符串转换为AuthPermissionList
        List<AuthRole> roleList = new Gson().fromJson(roleValue,
                new TypeToken<List<AuthPermission>>() {
                }.getType());
        // 将AuthPermissionList转换为stringList
        authList = roleList.stream().map(AuthRole::getRoleName).collect(Collectors.toList());

        // 返回结果
        return authList;
    }


}
